#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: ML-2D (DO NOT USE, OUTDATED)                                       #
#                                                                           #
# 2dx.org, GNU Plublic License.                                             #
#                                                                           #
# Created..........: 02/20/2006                                             #
# Last Modification: 07/18/2007                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 81
#
# MANUAL: <B>This script applies a Maximum Likelihood algorithm to a stack of unit cells.</B>
#
# MANUAL: <B>NOTE: The ML processing takes its parameters directly from the <i>2dx_image.cfg</I> file. You therefore have to <I>Save</I> the current parameters before starting this script.</B>
#
# MANUAL: For more information, see <A HREF="http://dx.doi.org/10.1016/j.jsb.2007.09.013">Zeng et al., JSB 2007</A>, or on <A HREF="http://2dx.org/documentation/2dx-software/parameters/Ml">2dx.org</A>.
#
# MANUAL: <IMG SRC="${appDir_2dx}/../config/2dx_ML.jpg">
#
# DISPLAY: SYN_Unbending
# DISPLAY: ML_doit
# DiSPLAY: ML_use_for_merging
# DISPLAY: ML_doMLorCC
# DISPLAY: RESMIN
# DISPLAY: RESMAX
# DISPLAY: SYM
# DISPLAY: defocus
# DISPLAY: stepdigitizer
# DISPLAY: magnification
# DISPLAY: ALAT
# DISPLAY: tempkeep
# DISPLAY: ctfrev
# DISPLAY: realang
# DISPLAY: realcell
# DISPLAY: phaori
# DISPLAY: phaori_ML
# DISPLAY: lattice
# DISPLAY: ML_realcellxy_outer
# DISPLAY: ML_mask_radius
# DISPLAY: ML_iteration
# DISPLAY: ML_threshold_method
# DISPLAY: ML_absolute_threshold
# DISPLAY: ML_relative_threshold
# DISPLAY: ML_ref_ind
# DISPLAY: ML_do_whiten
# DISPLAY: ML_correct_CTF
# DISPLAY: ML_rotational_symmetry
# DISPLAY: ML_MinMaxStep_angle
# DISPLAY: ML_terminate_ML
# DISPLAY: ML_lp_method
# DISPLAY: ML_lp_radius
# DISPLAY: ML_B_factor
# DISPLAY: ML_A_factor
# DISPLAY: comment
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set imagename = ""
set nonmaskimagename = ""
set imagenumber = ""
set defocus = ""
set imagenumber = ""
set imagesidelength = ""
set magnification = ""
set stepdigitizer = ""
set tempkeep = ""
set RESMIN = ""
set RESMAX = ""
set phacon = ""
set CS = ""
set KV = ""
set ALAT = ""
set lattice = ""
set realang = ""
set realcell = ""
set ctfrev = ""
set phaori = ""
set phaori_ML = ""
set lattice = ""
set SYN_Unbending = ""
set ML_doit = ""
set ML_doMLorCC = ""
set ML_realcellxy_outer = ""
set ML_mask_radius = ""
set ML_iteration = ""
set ML_threshold_method = ""
set ML_absolute_threshold = ""
set ML_relative_threshold = ""
set ML_ref_ind = ""
set ML_do_whiten = ""
set ML_correct_CTF = ""
set ML_rotational_symmetry = ""
set ML_MinMaxStep_angle = ""
set ML_terminate_ML = ""
set ML_lp_method = ""
set ML_lp_radius = ""
set ML_B_factor = ""
set ML_A_factor = ""
set rot180 = ""
set revhk = ""
set rot90 = ""
set beamtilt = ""
set zstarwin = ""
set TLTAXIS = ""
set TLTANG = ""
set TLTAXA = ""
set TAXA = ""
set TANGL = ""
set SYM = ""
set avrgamphsNUMBER = ""
set avrgamphsRESOL = ""
#
#$end_vars
#
echo "<<@progress: 1>>"
#
set scriptname = 2dx_max_likelihood
set ccp4_setup = 'y'
set IS_2DX = yes
source ${proc_2dx}/initialize
#
#echo ML_whitefirst = ${ML_whitefirst}
setenv OMP_NUM_THREADS 2
#source ~/.cshrc 
echo ML_doMLorCC = ${ML_doMLorCC}
#
set defocus_nokomma = `echo ${defocus} | sed 's/,/ /g'`
#set ML_oversizexy_nokomma = `echo ${ML_oversizexy} | sed 's/,/ /g'`
#set ML_realcellxy_inner_nokomma = `echo ${ML_realcellxy_inner} | sed 's/,/ /g'`
set ML_realcellxy_outer_nokomma = `echo ${ML_realcellxy_outer} | sed 's/,/ /g'`
set ML_MinMaxStep_angle_nokomma = `echo ${ML_MinMaxStep_angle} | sed 's/,/ /g'`
set lattice_nokomma = `echo ${lattice} | sed 's/,/ /g'`
set phaori_nokomma = `echo ${phaori} | sed 's/,/ /g'`
#
\rm -f LOGS/${scriptname}.results
#
echo "<<@evaluate>>"
#
if ( (${SYN_Unbending} != "0")) then
  ${proc_2dx}/linblock "Skipping. This script only runs if you don't have the Synthetical Reference chosen."
  echo "<<@progress: 100>>"
  exit
  #
endif
#
if ( ${ML_doit} == 'n' ) then
  ${proc_2dx}/linblock "No Maximum Likelihood processing."
  ${proc_2dx}/linblock "2dx_MaxLikelihood - normal end."
  echo "<<@progress: 100>>"
  exit
endif
#
${proc_2dx}/linblock "#"
echo "::   Attention: The ML software directly accesses the saved 2dx_image.cfg file."
echo "::   It doesn't get the data from the GUI of 2dx_image."
echo "::   You therefore must SAVE the current parameters, before running this script."
${proc_2dx}/linblock "#"
#
if ( ! -d ML ) then
  \mkdir ML
endif
#
if ( ${SYM} == "p2" || ${SYM} == "p12_a" || ${SYM} == "p12_b" || ${SYM} == "p121_a" || ${SYM} == "p121_b" || ${SYM} == "c12_a" || ${SYM} == "c12_b" || ${SYM} == "p222" || ${SYM} == "p2221a" || ${SYM} == "p2221b" || ${SYM} == "p22121" || ${SYM} == "c222" ) then
  if ( ${ML_rotational_symmetry} != "1" ) then
    echo ":: "
    echo ":: "
    ${proc_2dx}/linblock "#"
    ${proc_2dx}/linblock "WARNING: You did not set the 2-fold rotational symmetry. Is this an error?"
    ${proc_2dx}/linblock "#"
  endif
endif 
if ( ${SYM} == "p3" || ${SYM} == "p312" ) then
  if ( ${ML_rotational_symmetry} != "2" ) then
    echo ":: "
    echo ":: "
    ${proc_2dx}/linblock "#"
    ${proc_2dx}/linblock "WARNING: You did not set the 3-fold rotational symmetry. Is this an error?"
    ${proc_2dx}/linblock "#"
  endif
endif 
if ( ${SYM} == "p4" || ${SYM} == "p422" || ${SYM} == "p4212" ) then
  if ( ${ML_rotational_symmetry} != "3" ) then
    echo ":: "
    echo ":: "
    ${proc_2dx}/linblock "#"
    ${proc_2dx}/linblock "WARNING: You did not set the 4-fold rotational symmetry. Is this an error?"
    ${proc_2dx}/linblock "#"
  endif
endif 
if ( ${SYM} == "p6" || ${SYM} == "p622" ) then
  if ( ${ML_rotational_symmetry} != "5" ) then
    echo ":: "
    echo ":: "
    ${proc_2dx}/linblock "#"
    ${proc_2dx}/linblock "WARNING: You did not set the 6-fold rotational symmetry. Is this an error?"
    ${proc_2dx}/linblock "#"
  endif
endif 
#
if ( "now" == "future" ) then
  echo ":: "
  ${proc_2dx}/linblock "Sourcing 2dx_ML_stack_sub.com script"
  echo ":: "
  # source ${proc_2dx}/2dx_ML_stack_sub.com
  #
  #############################################################################
  echo ":: "
  ${proc_2dx}/linblock "Sourcing 2dx_ML_run_sub.com script"
  echo ":: "
  # source ${proc_2dx}/2dx_ML_run_sub.com
  #
else
  #
  #############################################################################
  echo ":: "
  ${proc_2dx}/linblock "Sourcing 2dx_ML_sub.com script"
  echo ":: "
  source ${proc_2dx}/2dx_ML_sub.com
  #
endif
#############################################################################
#############################################################################
#############################################################################
#
set aphfile = "APH/ML_result.aph"
if ( ! -e ${aphfile} ) then
  ${proc_2dx}/protest "ERROR: ${aphfile} does not exist. Problem in ML."
endif
#
echo "set ML_done = y" >> LOGS/${scriptname}.results
#
echo ":: Done."
echo "<<@progress: 100>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
exit
#
#
